import { useState, useEffect, useCallback } from "react";
import { message } from "antd";

const useTableData = ({
  initPage = 1,
  initMaxResult = 20,
  queryApiAndOtherParams: { queryApi, otherParams },
}) => {
  const [page, setPage] = useState(initPage);
  const [maxResult, setMaxResult] = useState(initMaxResult);
  const [queryForm, setQueryForm] = useState({});
  const [responseData, setResponseData] = useState({});
  const getJokesData = useCallback(async () => {
    const responseData = await queryApi({
      page,
      maxResult,
      ...otherParams,
      ...queryForm,
    });
    if (responseData.showapi_res_code === 0) {
      setResponseData(responseData.showapi_res_body);
    } else message.error(responseData.showapi_res_error);
  }, [maxResult, otherParams, page, queryApi, queryForm]);

  useEffect(() => getJokesData(), [page, maxResult, queryForm, getJokesData]);

  return [
    page,
    setPage,
    maxResult,
    setMaxResult,
    queryForm,
    setQueryForm,
    responseData.contentlist || [],
    responseData.allNum,
  ];
};

export default useTableData;
